package com.saret.dictionary;

import com.saret.crawler.search.BingWebSearch;
import com.saret.crawler.search.DownloadPage;
import com.saret.crawler.search.SmartWebSearch;
import com.saret.dictionary.type.Sentence;
import com.saret.utils.FileLocator;
import com.saret.utils.StringUtils;
import com.saret.utils.UtfFileHandle;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Matcher {

    static DownloadPage downloadPage = new DownloadPage();
    static SmartWebSearch smartWebSearch = new SmartWebSearch(new BingWebSearch("2943E37C2C98C49C2FA2AC5EAF953F8861F81385"));


    public Matcher(String aUrl, String bUrl, String type) {
          List<Sentence> sentenceList = match(aUrl, bUrl,type);
        SaveResult  saveResult = new SaveResult();
        saveResult.addWord(bUrl, Arrays.asList(aUrl));
        for (Sentence sentence : sentenceList) {

        saveResult.addExample(sentence.getLn2(),aUrl,sentence.getLn1(),bUrl,sentence.getScore());

        }
        saveResult.save(new File(FileLocator.getTranslateDir(),"tranlated.xml"));

    }

    public  List<Sentence> match(String aUrl, String bUrl, String type) {
        File f1 = downloadPage.downloadAsText(aUrl, "t1ab");
        File f2 = downloadPage.download(bUrl, "t12ab", type);
        return match(f1, f2);

    }

    public List<Sentence> match(File a, File b) {
        List<String> originalEnglish = normalize(a);  //english
        List<String> originalDutch = normalize(b);  //dutch
        List<String> tranlatedDutch = smartWebSearch.translate(originalEnglish);   //english translated to dutch
        UtfFileHandle.write(new File(a.getParentFile(), "Translated" + a.getName()), tranlatedDutch);
        List<Sentence> sentences = new ArrayList<Sentence>();

        for (String originalDutchLine : originalDutch) {
            double bScore = 0;
            String translation = "";
            for (int i = 0; i < tranlatedDutch.size(); i++)
            //for (String aLine : originalEnglish)
            {

                double bScore0 = Score.score(originalDutchLine, Arrays.asList(tranlatedDutch.get(i).split("\\s+")));
                if (bScore0 > bScore) {
                    translation = originalEnglish.get(i);
                    bScore = bScore0;
                }
            }
            if (bScore > 0.5) {
                sentences.add(new Sentence(originalDutchLine, translation, bScore));
            }
        }
        return sentences;
    }

    public static void main(String[] args) {
        /*  List<String> a = new ArrayList<String>();
         a.add("Hello People. What is this?");
         a.add("Can you pass me a milk, Biniam23");
         smartWebSearch.translate(a);
        */

        new Matcher("http://en.wikipedia.org/wiki/Rotterdam","http://nl.wikipedia.org/wiki/Rotterdam", "html");
       // match("http://en.wikipedia.org/wiki/Rotterdam", "http://nl.wikipedia.org/wiki/Rotterdam", "html");
    }

    private static int min(int a, int b, int c) {
        return min(a, b) < c ? min(a, b) : c;
    }

    private static int min(int a, int b) {
        return a < b ? a : b;
    }

    private static List<String> normalize(File f) {
        List<String> lines = new ArrayList<String>();
        for (String s : UtfFileHandle.readFileToListOfLines(f)) {

            String[] words = s.replace("\n", "").split("\\s+");
            StringBuilder builder = new StringBuilder();
            for (String word : words) {
                boolean valid = StringUtils.match(word.trim(), "^[<|>]", 0) == null;
                if (valid && word.length() < 25)
                    builder.append(word).append(" ");
            }
            builder.append("\n");
            String goodLine = builder.toString();
            if (goodLine.length() > 100) {
                lines.add(builder.toString());
            }
        }
        return lines;
    }
}
